<template>
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-12">
        <b-card header="<i class='fa fa-align-justify'></i> 咨询信息">
          <div class="search-box">
            <input type="text" class="form-control"  placeholder="公司名" v-model="companyName">
            <b-button variant="outline-primary" @click="searchConsult()">搜索</b-button>
          </div>
          <div class="table-box">
            <table class="table table-striped">
              <thead>
              <tr>
                <th>序号</th>
                <th>姓名</th>
                <th>公司名称</th>
                <th>部门名称</th>
                <th>邮政编码</th>
                <th>地址</th>
                <th>电话</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              <tr>
                <td colspan="8" v-show="isConsult" style="text-align: center;">没有搜索到匹配的信息!</td>
              </tr>
              <tr v-for="(item, index) in currentData">
                <td>{{ index + 1 + number }}</td>
                <td>{{ item.name }}</td>
                <td>{{ item.companyName }}</td>
                <td>{{ item.deptName }}</td>
                <td>{{ item.postcode }}</td>
                <td>{{ item.address }}</td>
                <td>{{ item.tel }}</td>
                <td>
                  <span class="badge badge-success" @click="updateConsult(item.id)">详情</span>
                  <span class="badge badge-danger" @click="deleteConsult(item.cid)">删除</span>
                </td>
              </tr>
              </tbody>
            </table>
          </div>
          <el-pagination
            layout="prev, pager, next"
            :current-page.sync="current"
            :page-size="10"
            :total="total">
          </el-pagination>
        </b-card>
      </div>
    </div>
    <div title="Modal title" class="modal-primary" v-model="primaryModal" v-show="primaryModal">
      <span class="modal-open">
        <div class="modal fade show" role="dialog">
          <div class="modal-dialog modal-md">
            <div tabindex="-1" role="document" class="modal-content">
              <header class="modal-header">
                <h5 class="modal-title">删除信息</h5>
                <button type="button" aria-label="Close" class="close" @click="cancelModal()">
                  <span aria-hidden="true">×</span>
                </button>
              </header>
              <div class="modal-body">
                <br>是否删除该条咨询信息？<br><br>
              </div>
              <footer class="modal-footer">
                <button type="button" class="btn btn-primary btn-md" @click="cancelModal()">取消</button>
                <button type="button" class="btn btn-danger btn-md" @click="suredeleteConsult()">确认</button>
              </footer>
            </div>
          </div>
        </div>
        <div class="modal-backdrop fade show"></div>
      </span>
    </div>
  </div>
</template>

<script>
  import { findCompanyConsult, deleteCompany, likeCompany } from '../../service/api'
  import _ from 'lodash'
  export default {
    name: 'tables',
    data () {
      return {
        current: 1,
        number: 0,
        consultList: [],
        pageNumber: '',
        companyName: '',
        primaryModal: false,
        isConsult: false,
        delId: ''
      }
    },
    computed: {
      currentData () {
        return this.consultList[this.current - 1]
      },
      total () {
        return ~~this.pageNumber
      }
    },
    methods: {
      cancelModal () {
        this.primaryModal = false
      },
      updateConsult (index) {
        this.$router.push({
          path: '/information/consult_detail?id=' + index
        })
      },
      deleteConsult (index) {
        this.delId = index
        this.primaryModal = true
      },
      suredeleteConsult () {
        deleteCompany({id: this.delId})
          .then(({ data, err }) => {
            console.log('err: ' + err)
            console.log('data: ' + data)
            if (data.code === 200) {
              this.primaryModal = false
              alert('删除成功!')
              location.reload()
            }
          })
      },
      searchConsult () {
        console.log('companyName: ' + this.companyName)
        if (this.companyName === '' || !this.companyName.trim()) {
          alert('搜索内容不能为空!')
        } else {
          likeCompany({name: this.companyName})
            .then(({ data, err }) => {
              console.log('err: ' + err)
              console.log('data: ' + data)
              if (data.code === 200) {
                this.pageNumber = data.result.length
                this.current = 1
                this.consultList = _.chunk(data.result, 10)
                if (data.result.length === 0) {
                  this.isConsult = true
                } else {
                  this.isConsult = false
                }
              }
            })
        }
      },
      handleCurrentChange () {
        console.log('handleCurrentChange: ' + this.current)
        if (this.current === 1) {
          this.number = 0
        } else {
          this.number = (this.current - 1) * 10
        }
      }
    },
    created () {
      // fetch
      findCompanyConsult()
        .then(({ data, err }) => {
          console.log('err: ' + err)
          console.log('data: ' + data)
          if (data.code === 200) {
            this.pageNumber = data.result.length
            this.consultList = _.chunk(data.result, 10)
            if (data.result.length === 0) {
              this.isConsult = true
            } else {
              this.isConsult = false
            }
          }
        })
    }
  }
</script>
<style type="text/scss" rel="stylesheet/scss" lang="scss" scoped>
  @import "../../styles/common";
</style>
